﻿using System;
using System.Collections.Generic;
using JStore.Core.Dtos;
using JStore.Core.Entities;
using SpeadFrm.MvcPager;
using SpeadFrm.ORM;

namespace JStore.Data.Daos
{
    /// <summary>
    ///
    /// </summary>
    public class MoneyLogDao : BaseDao<MoneyLog>
    {
        public PagedList<MoneyLog> GetPagedList(string userName, string sourceUserName, int? moneyType, int pageIndex, int pageSize)
        {
            var sql = Sql.Builder.Where("1=1");
            if (!string.IsNullOrEmpty(userName)) sql.Where("UserName = @0", userName);
            if (!string.IsNullOrEmpty(sourceUserName)) sql.Where("SourceUserName = @0", sourceUserName);
            if (moneyType != null) sql.Where("MoneyType = @0", moneyType);
            sql.OrderBy("CreateDate DESC");
            return PagedList(pageIndex, pageSize, sql);
        }

        public MoneyLog GetById(int id)
        {
            var sql = Sql.Builder.Where("Id = @0", id);
            return FirstOrDefault(sql);
        }

        public decimal GetSum(DateTime startDate, DateTime endDate)
        {
            var sql = Sql.Builder
                .Append("SELECT ISNULL(SUM(RealMoney),0.00)")
                .Append("FROM MoneyLog")
                .Where("DATEDIFF(DAY,CreateDate,@0) <=0 AND DATEDIFF(DAY,CreateDate,@1) >= 0", startDate, endDate);
            return FirstOrDefault<decimal>(sql);
        }
         
    }
}

